package handler

import (
	"github.com/gin-gonic/gin"
	"onerk4/zk3/api/client"
	"onerk4/zk3/api/enit/request"
	"onerk4/zk3/api/enit/response"
	"onerk4/zk3/common/proto/user"
)

func Register(c *gin.Context) {
	var req request.RegisterOrLogin

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	register, err := client.Register(c, &user.RegisterOrLoginRequest{
		Mobile:   req.Mobile,
		Password: req.Password,
	})
	if err != nil {
		response.ErrorResponse(c, "fail", err.Error())
		return
	}
	response.SuccessResponse(c, "ok", register)
}

func Login(c *gin.Context) {
	var req request.RegisterOrLogin

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	register, err := client.Login(c, &user.RegisterOrLoginRequest{
		Mobile:   req.Mobile,
		Password: req.Password,
	})
	if err != nil {
		response.ErrorResponse(c, "fail", err.Error())
		return
	}
	response.SuccessResponse(c, "ok", register)
}

func AddStudyPlanShow(c *gin.Context) {
	var req request.AddOrDel

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	userId := c.GetUint("userId")
	register, err := client.AddStudyPlanShow(c, &user.AddStudyPlanShowRequest{
		UserId:      int64(userId),
		StudyPlanId: req.StudyPlanId,
	})
	if err != nil {
		response.ErrorResponse(c, "fail", err.Error())
		return
	}
	response.SuccessResponse(c, "ok", register)
}

func DelStudyPlanShow(c *gin.Context) {
	var req request.AddOrDel

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	userId := c.GetUint("userId")
	register, err := client.DelStudyPlanShow(c, &user.DelStudyPlanShowRequest{
		UserId:      int64(userId),
		StudyPlanId: req.StudyPlanId,
	})
	if err != nil {
		response.ErrorResponse(c, "fail", err.Error())
		return
	}
	response.SuccessResponse(c, "ok", register)
}

func CreateOrder(c *gin.Context) {
	var req request.AddOrDel

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	userId := c.GetUint("userId")
	register, err := client.CreateOrder(c, &user.CreateOrderRequest{
		UserId:      int64(userId),
		StudyPlanId: req.StudyPlanId,
	})
	if err != nil {
		response.ErrorResponse(c, "fail", err.Error())
		return
	}
	response.SuccessResponse(c, "ok", register)
}
